import 'package:flutter/material.dart';
import 'package:super_study_english_tool/dao/dao_user_album_media.dart';
import 'package:super_study_english_tool/screen/screen_player.dart';
import 'package:super_study_english_tool/service/service_user_album_media.dart';
import 'package:super_study_english_tool/widget/widget_album_media.dart';
import '../model/model_user_album_media.dart';

// 专辑媒体列表面板
class PanelAlbumMediaList extends StatefulWidget {
  const PanelAlbumMediaList({super.key, required this.albumId});

  final int albumId;

  @override
  State<PanelAlbumMediaList> createState() => _PanelAlbumMediaListState();
}

class _PanelAlbumMediaListState extends State<PanelAlbumMediaList> {
  List<ModelUserAlbumMedia> medias = [];

  @override
  void initState() {
    super.initState();
    // medias = DaoUserAlbumMedia.findAllByAlbumId(widget.albumId);
    medias = ServiceUserAlbumMedia.listByAlbumId(widget.albumId);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("专辑媒体列表"),
      ),
      body: buildList(),
    );
  }

  buildList() {
    print("${medias.length}");
    return ListView.builder(
        itemCount: medias.length,
        itemExtent: 50.0, //强制高度为50.0
        itemBuilder: (BuildContext context, int index) {
          String name = medias[index].fileName!;
          return WidgetAlbumMedia(
            media: medias[index],
            onPressed: (id) {
              print(id);

              // Navigator.of(context).push(MaterialPageRoute(builder: (context) {
              //   return PanelAlbumMediaList(
              //     albumId: id,
              //   );
              // }));
            },
            onBack: (id) {
              // medias = DaoUserAlbumMedia.findAllByAlbumId(widget.albumId);
              medias = ServiceUserAlbumMedia.listByAlbumId(widget.albumId);
              setState(() {});
            },
          );
          // ListTile(title: Text("$name"));
        });
  }

  @override
  void didUpdateWidget(covariant PanelAlbumMediaList oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("1 didUpdateWidget");
  }

  @override
  void deactivate() {
    super.deactivate();
    print("1 deactivate");
  }

  @override
  void dispose() {
    super.dispose();
    print("1 dispose");
  }

  @override
  void reassemble() {
    super.reassemble();
    print("1 reassemble");
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("1 didChangeDependencies");
  }
}
